﻿using BLL.Interface;
using Common.Extensions;
using DAL.Interface;
using Microsoft.EntityFrameworkCore;
using Model;
using Model.DTO.Comment;
using Model.DTO.Public.GetList;
using Model.DTO.Public.OperateId;
using Model.Entity.Article;
using Model.Entity.Comment;
using Model.Entity.User;

namespace BLL
{
    /// <summary>
    /// 评论业务逻辑层
    /// </summary>
    public class CommentBLL : ICommentBLL
    {
        private readonly IBaseDAL_LONG<TB_Comment> _commentDAL;
        private readonly IBaseDAL_LONG<TB_Article> _articleDAL;
        private readonly IBaseDAL<TB_User> _userDAL;
        public CommentBLL(
            IBaseDAL_LONG<TB_Comment> commentDAL,
            IBaseDAL_LONG<TB_Article> articleDAL,
            IBaseDAL<TB_User> userDAL
            )
        {
            _commentDAL = commentDAL;
            _articleDAL = articleDAL;
            _userDAL = userDAL;
        }
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> AddComment(AddCommentDTO param)
        {
            try
            {
                #region 过滤
                var user = await _userDAL.GetEntityAllAsync(x => x.Id == param.UserId).Result.FirstOrDefaultAsync();
                if (user == null)
                {
                    return ResponseMessage.CreateError("用户Id无效");
                }
                var articleIdIsNull = param.ArticleId == null;
                var commentIdIsNull = param.CommentId == null;
                if (articleIdIsNull && commentIdIsNull)
                {
                    return ResponseMessage.CreateError("目标Id无效");
                }
                if (!articleIdIsNull)
                {
                    var existArticle = await _articleDAL.GetEntityAllAsync(x => x.Id == param.ArticleId).Result.AnyAsync();
                    if (!existArticle)
                    {
                        return ResponseMessage.CreateError("文章不存在");
                    }
                }
                if (!commentIdIsNull)
                {
                    var existComment = await _articleDAL.GetEntityAllAsync(x => x.Id == param.CommentId).Result.AnyAsync();
                    if (!existComment)
                    {
                        return ResponseMessage.CreateError("评论不存在");
                    }
                }
                #endregion
                var comment = new TB_Comment();
                param.CopyPropertiesTo(comment);
                var result = await _commentDAL.AddEntityAsync(comment);
                return result ? ResponseMessage.CreateSuccess("添加成功") : ResponseMessage.CreateError("添加失败");
            }
            catch
            {
                return ResponseMessage.CreateError("添加失败");
            }
        }
        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> DeleteComment(OperateIdDTO_LONG param)
        {
            try
            {
                bool result;
                if (param.Id > 0)
                {
                    param.Ids.Clear();
                    param.Ids.Add(param.Id);
                    param.Id = 0;
                    return await DeleteComment(param);
                }
                #region 过滤
                if (param.Ids.Count <= 0)
                {
                    return ResponseMessage.CreateError("评论Id无效");
                }
                #endregion
                using var tran = await _commentDAL.BeginTransactionAsync();
                try
                {
                    foreach (var id in param.Ids)
                    {
                        result = await _commentDAL.DeleteEntityAsync(id);
                    }
                    await tran.CommitAsync();
                    return ResponseMessage.CreateSuccess("删除成功");
                }
                catch
                {
                    await tran.RollbackAsync();
                    throw;
                }
            }
            catch
            {
                return ResponseMessage.CreateError("删除失败");
            }
        }
        /// <summary>
        /// 编辑评论
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> EditComment(EditCommentDTO param)
        {
            try
            {
                #region 过滤
                var comment = await _commentDAL.GetEntityAllAsync(x => x.Id == param.Id).Result.FirstOrDefaultAsync();
                if (comment == null)
                {
                    return ResponseMessage.CreateError("评论不存在");
                }
                #endregion
                param.CopyPropertiesTo(comment);
                var result = await _commentDAL.EditEntityAsync(comment);
                return result ? ResponseMessage.CreateSuccess("编辑成功") : ResponseMessage.CreateError("编辑失败");
            }
            catch
            {
                return ResponseMessage.CreateError("编辑失败");
            }
        }
        /// <summary>
        /// 获取评论列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> GetCommentList(GetListDTO param)
        {
            try
            {
                #region 参数校验
                if (param.Page <= 0)
                {
                    param.Page = 1;
                }
                if (param.Size <= 0)
                {
                    param.Size = 10;
                }
                #endregion
                var comments = await _commentDAL.GetEntityAllAsync(x => x.Content.Contains(param.Query ?? string.Empty));
                var total = await comments.CountAsync();
                var list = await comments.Skip((param.Page - 1) * param.Size).Take(param.Size).ToListAsync();
                return ResponseMessage.CreateSuccess("获取成功", new { total, list });
            }
            catch
            {
                return ResponseMessage.CreateError("获取失败");
            }
        }
    }
}
